package com.itheima.controller;

import com.itheima.domain.pojo.Student;
import com.itheima.domain.query.QueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.Result;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:篍
 * @Date: 2025/07/14 18:38
 * @Description:
 */
@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 分页查询所有学员
     * @return
     */
    @GetMapping
    public Result pageListStudents(QueryParam queryParam){
        PageResult<Student> studentPageResult =studentService.pageListStudents(queryParam);
        return Result.success(studentPageResult);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        studentService.addStudent(student);
        return Result.success();
    }

    /**
     * 根据id查询学生
     * @param id
     * @return学生
     */
    @GetMapping("{id}")
    public Result findStudentById(@PathVariable("id") Integer id){
        Student student=studentService.findStudentById(id);
        return Result.success(student);
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 根据id数组删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public Result deleteStudent(@PathVariable("ids") Integer[] ids){
        studentService.deleteStudent(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violationScore(@PathVariable("id") Integer id,
                                 @PathVariable("score") Short score){
        log.warn("id={},score={}",id,score);
        studentService.violationScore(id,score);
        return Result.success();
    }


}
